﻿/************************************************************************
*  Copyright @   All rights reserved.
*  GUID: 66d98a3f-a50e-4396-9ce6-72b2a26e6e1f
*  计算机用户域： XIANGLIUBO-PC
*  计算机用户名： WuHao
*  创建者： 向柳波
*  创建时间： 2015-01-28 10:52:26
*  创建说明： 
*  ---------------------------------------------------------------------
*  更新者： 
*  更新时间： 2015-01-28 10:52:26
*  更新说明： 
************************************************************************/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CommonHelp {
    public class HttpHelper {
        string _url;

        public string Url {
            get {
                return _url;
            }
            set {
                _url = value;
            }
        }

        public HttpHelper() {
        }

        public HttpHelper(string url) {
            _url = url;
        }

        public void GetAsync(Action<string> callback) {
            this.GetAsync(callback, _url, "GET");
        }

        public void GetAsync(Action<string> callback, string method) {
            this.GetAsync(callback, _url, method);
        }

        public void GetAsync(Action<string> callback, string url, string method) {
            string retString = null;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            Task<WebResponse> taskWebResponse = Task.Factory.FromAsync<WebResponse>(request.BeginGetResponse,
                                     request.EndGetResponse,
                                     null);
            taskWebResponse.ContinueWith(task => {
                try {
                    var response = (HttpWebResponse)task.Result;

                    using (Stream myResponseStream = response.GetResponseStream()) {
                        StreamReader myStreamReader = new StreamReader(myResponseStream);
                        retString = myStreamReader.ReadToEnd();
                        myStreamReader.Close();
                        myResponseStream.Close();
                    }
                }
                catch (Exception ex) {
                    CommonHelp.LogHelper.Error(ex);
                }
                finally {
                    callback(retString);
                }
            });
        }

        public void POSTAsync(Action<string> callback, Dictionary<string, string> parmeters) {
            this.POSTAsync(callback, parmeters, _url);
        }

        public void POSTAsync(Action<string> callback, Dictionary<string, string> parmeters, string url) {
            NameValueCollection @params = System.Web.HttpUtility.ParseQueryString(String.Empty);
            foreach (var item in parmeters) {
                @params.Add(item.Key, item.Value);
            }
            byte[] data = UTF8Encoding.UTF8.GetBytes(@params.ToString());

            Task.Factory.StartNew(delegate {
                WebRequest webRequest = WebRequest.Create(url);
                webRequest.Method = WebRequestMethods.Http.Post;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.ContentLength = data.Length;

                Stream newStream = webRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);//写入参数
                newStream.Close();

                webRequest.BeginGetResponse(delegate(IAsyncResult result) {
                    string responseStr = null;
                    try {
                        WebResponse response = webRequest.EndGetResponse(result);
                        using (Stream stream = response.GetResponseStream()) {
                            //stream.Seek(0, SeekOrigin.Begin);
                            StreamReader sr = new StreamReader(stream);
                            responseStr = sr.ReadToEnd();
                        }
                    }
                    catch (Exception ex) {
                        CommonHelp.LogHelper.Error(ex);
                    }
                    finally {
                        callback(responseStr);
                    }
                }, null);
            });
        }

        //public void POSTAsync(Action<Object> callback, params string url) {
        //    this.POSTAsync(delegate(string rst) {
        //        Newtonsoft.Json.JsonConverter jsc = new Newtonsoft.Json.JsonConverter();
        //        jsc.ReadJson
        //    }); 
        //}
    }
}
